using UnityEngine;
using UnityEngine.UI;

public class AboutPanel : MonoBehaviour
{
    public GameObject About;
    bool isNeedAnimation = false, showing = false;
    Vector2 oriSize;
    float runTime;

    public GameObject Content, Text, Link;
    // Start is called before the first frame update
    void Start()
    {
        About.SetActive(false);
        oriSize = new Vector2(About.GetComponent<RectTransform>().rect.x, About.GetComponent<RectTransform>().rect.y);
        About.GetComponent<RectTransform>().sizeDelta = 0.3f * oriSize;
        About.GetComponent<CanvasGroup>().alpha = 0;
        SetContentSize();
    }

    // Update is called once per frame
    void Update()
    {
        if(isNeedAnimation)
        {
            runTime += Time.deltaTime;
            if(!showing)
            {
                About.GetComponent<CanvasGroup>().alpha = 4 * runTime;
                About.GetComponent<RectTransform>().sizeDelta = (1 - 4 * runTime) * oriSize * 0.3f;
                if(runTime >= 0.25f)
                {
                    About.GetComponent<CanvasGroup>().alpha = 1;
                    About.GetComponent<RectTransform>().sizeDelta = new Vector2(0, 0);
                    isNeedAnimation = true;
                    showing = false;
                }
            }
            else
            {
                About.GetComponent<CanvasGroup>().alpha = 1 - 4 * runTime;
                About.GetComponent<RectTransform>().sizeDelta = 1.2f * runTime * oriSize;
                if(runTime >= 0.25f)
                {
                    About.GetComponent<CanvasGroup>().alpha = 0;
                    About.GetComponent<RectTransform>().sizeDelta = 0.3f * oriSize;
                    isNeedAnimation = false;
                    showing = true;
                    About.SetActive(false);
                }
            }
        }
    }

    public void ShowAboutPanel()
    {
        About.SetActive(true);
        isNeedAnimation = true;
        showing = false;
        runTime = 0;
    }

    public void HideAboutPanel()
    {
        isNeedAnimation = true;
        showing = true;
        runTime = 0;
    }

    public void OpenLink()
    {
        Application.OpenURL("https://ys.mihoyo.com/");
    }

    public void SetContentSize()
    {
        float width = Content.GetComponent<RectTransform>().rect.width;
        Content.GetComponent<RectTransform>().sizeDelta = new Vector2(-20, width * 1928 / 1440 + width * 0.6f * 316 / 1232);
        Text.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -width * 1928 / 2880);
        Text.GetComponent<RectTransform>().sizeDelta = new Vector2(width, width * 1928 / 1440);
        Link.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -width * 1928 / 1440 - width * 0.3f * 316 / 1232);
        Link.GetComponent<RectTransform>().sizeDelta = new Vector2(width, width * 0.6f * 316 / 1232);
    }
}
